<?php
/**
 * 
 * @author binhtv
 *
 */
class cms_View_Helper_Translate {
	public $view;
	private static $_translate;
	
	public function translate($key, $locale=null)
	{
	    $currentLocale = $locale;
	    if(!$currentLocale) {
	        $currentLocale = $_SESSION['locale'];
	        if(!$currentLocale) {
	            $currentLocale = LOCALE;
	        }
	    }
	    if(!self::$_translate) {
    	    $translate = new Zend_Translate(
                array(
                    'adapter' => 'ini',
                    'locale'  => $currentLocale,
                )
            );
    	    
    	    $files = Utils_Global::getConfig('cms', 'application', 'languages');
    	    if ($files) {
    	        $files = $files->toArray();
    	        foreach ($files as $lc => $file) {
    	            if(is_array($file)) {
    	                foreach ($file as $f) {
        	                $translate->addTranslation(
        	                		array(
        	                				'content' => $f,
        	                				'locale'  => $lc,
        	                		)
        	                );
    	                }
    	            }
    	        }
    	    }
    	    self::$_translate = $translate;
	    }
	    
	    self::$_translate->setLocale($currentLocale);
	    return self::$_translate->_($key);
	}

	public function setView(Zend_View_Interface $view)
	{
		$this->view = $view;		
	}

}